<?php
/**
 * User: Huy Nguyen
 * Date: 2/23/13
 * Time: 10:41 AM
 */
class UsersController extends Zend_Controller_Action
{
    public function userloginAction()
    {
        $tran = Zend_Registry::get('Zend_Translate');
        $ses = new Zend_Session_Namespace(SESSION_NSPACE);
        if($ses -> ready == SECURE_HASH ."OK"){
            $err = array(
                "msg" => $tran->translate('_5'),
                "status" => -1
            );
            echo Zend_Json::encode($err);
            exit();
        }
        if (!$this->_request->isXmlHttpRequest()) {
            throw new Zend_Controller_Action_Exception($tran->translate('_404'), 404);
        }
        ;
        $post = $this->getRequest()->getPost();
        if ($post) {
            $db = Zend_Registry::get('db');
            $tbl_user = new Zend_Db_Table("tbluser");
            $row = $tbl_user->select();
            $row->where("userName=?", $post["username"])
                ->where("password=?", md5($post["password"]));
            $user = $tbl_user->fetchRow($row);
            if (empty($user)) {
                $err = array(
                    "msg" => $tran->translate('_5'),
                    "status" => -1
                );
            } else {
                $ua = $user->toArray();
                $err = array(
                    "msg" => $tran->translate('_6'),
                    "status" => 1,
                    "username" => $ua["userName"],
                    "fullName" => $ua["userFullName"]
                );
                $ses -> ready = SECURE_HASH ."OK";
            }
            echo Zend_Json::encode($err);
            $this->_helper->viewRenderer->setNoRender(true);
            exit();
        } else {
            $this->view->SkipLayout = true;
        }
        return $this->view;
    }
    public function registorAction(){
        $tran = Zend_Registry::get('Zend_Translate');
        $ses = new Zend_Session_Namespace(SESSION_NSPACE);
        if($ses -> ready == SECURE_HASH ."OK"){
            $err = array(
                "msg" => $tran->translate('_7'),
                "status" => -1
            );
            echo Zend_Json::encode($err);
            exit();
        }
        if (!$this->_request->isXmlHttpRequest()) {
            throw new Zend_Controller_Action_Exception($tran->translate('_404'), 404);
        };
        $post = $this->getRequest()->getPost();
        if ($post) {
            $db = Zend_Registry::get('db');
            $tbl_user = new Zend_Db_Table("tbluser");
            $err = array(
                "msg" => "",
                "status" => -1
            );
            if($post["username"]["data"] == "")
            {
                $err["msg"] = $tran->translate("_8");
                echo Zend_Json::encode($err);
                exit();
            }elseif(strlen($post["username"]["data"]) < 3 ||strlen($post["username"]["data"])>255 ){
                $err["msg"] = $tran->translate("_1");
                echo Zend_Json::encode($err);
                exit();
            }
            if($post["fullname"]["data"] == "")
            {
                $err["msg"] = $tran->translate("_9");
                echo Zend_Json::encode($err);
                exit();
            }
            $row =  $tbl_user ->select();
            $row->where("username=?",$post["username"]["data"]);
            $user = $tbl_user->fetchRow($row);
            if($user){
                $err["msg"] = $tran->translate("_2");
                echo Zend_Json::encode($err);
                exit();
            }
            $row =  $tbl_user ->select();
            $row->where("email=?",$post["email"]["data"]);
            $mail = $tbl_user->fetchRow($row);
            if($mail){
                $err["msg"] = $tran->translate("_3");
                echo Zend_Json::encode($err);
                exit();
            }
            $tbl_user -> insert(
                array(
                    "username" => $post["username"]["data"],
                    "password" => md5($post["userpass"]["data"]),
                    "email" => $post["email"]["data"],
                    "phone" => $post["phone"]["data"],
                    "fullname" => $post["fullname"]["data"]
                )
            );
            $err["msg"] = $tran->translate("_10");
            $err["status"] =1;
            echo Zend_Json::encode($err);
            $this->_helper->viewRenderer->setNoRender(true);
            $this->view->SkipLayout = true;
            return $this -> view;
        }else{
            $this->view->SkipLayout = true;
            return $this -> view;
        }
    }
    public function logoutAction()
    {
        $ses = new Zend_Session_Namespace(SESSION_NSPACE);
        unset($ses->ready);
        Zend_Session::namespaceUnset(SESSION_NSPACE);
        $this->view->SkipLayout = true;
        exit();
    }
}
